package com.slm.report.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.othershe.nicedialog.BaseNiceDialog;
import com.othershe.nicedialog.NiceDialog;
import com.othershe.nicedialog.ViewConvertListener;
import com.othershe.nicedialog.ViewHolder;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.database.transaction.QueryTransaction;
import com.slm.mylibrary.base.BaseFragment;
import com.slm.mylibrary.utils.DialogUtils;
import com.slm.mylibrary.utils.IDialogAlertCallback;
import com.slm.report.R;
import com.slm.report.adapter.VisionListAdapter;
import com.slm.report.databinding.FragmentAddListBinding;
import com.slm.report.db.ContentDataBean;
import com.slm.report.db.StudentInfoBean;
import com.slm.report.db.VisionDataInfo;
import com.slm.report.db.VisionDataInfo_Table;
import com.slm.report.state.StudentInfoViewModel;
import com.slm.report.view.data.VisionContentView;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: My Application
 * @Package: com.slm.report.ui.fragment
 * @ClassName: VisionListFragment
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2020/6/3 17:27
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/6/3 17:27
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class VisionListFragment extends BaseFragment<StudentInfoViewModel> {
    private FragmentAddListBinding viewBinding;
    private StudentInfoBean studentInfoBean;
    private VisionListAdapter visionListAdapter;
    private List<VisionDataInfo> visionDataInfos = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentAddListBinding.inflate(LayoutInflater.from(getContext()));
        visionListAdapter = new VisionListAdapter(getContext(), visionDataInfos, new VisionListAdapter.OnDelDataCallBack() {
            @Override
            public void deleteData(final VisionDataInfo dataInfo) {
                DialogUtils.obtainCommonDialog("确定删除信息？", new IDialogAlertCallback() {
                    @Override
                    public void sure() {
                        dataInfo.delete();
                        visionDataInfos.remove(dataInfo);
                        visionListAdapter.notifyDataSetChanged();
                        ToastUtils.showShort("删除成功");
                        mViewModel.isChangeVision.setValue(true);
                    }
                }).show(getChildFragmentManager());
            }
        });
        viewBinding.listData.setAdapter(visionListAdapter);
        mViewModel.studentInfoBean.observe(this, new Observer<StudentInfoBean>() {
            @Override
            public void onChanged(StudentInfoBean infoBean) {
                studentInfoBean = infoBean;
                obtainData();
            }
        });

        viewBinding.addBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAddView();
            }
        });

        return viewBinding.getRoot();
    }

    private void obtainData() {
        SQLite.select()
                .from(VisionDataInfo.class)
                .where(VisionDataInfo_Table.studentId.eq(studentInfoBean.getId()))
                .orderBy(VisionDataInfo_Table.createTime, false)
                .async()
                .queryListResultCallback(new QueryTransaction.QueryResultListCallback<VisionDataInfo>() {
                    @Override
                    public void onListQueryResult(QueryTransaction transaction, @NonNull List<VisionDataInfo> tResult) {
                        visionDataInfos.clear();
                        visionDataInfos.addAll(tResult);
                        visionListAdapter.notifyDataSetChanged();
                    }
                })
                .execute();

    }

    private void addVision(ContentDataBean luoData, ContentDataBean jiaoData) {
        if (studentInfoBean != null) {
            VisionDataInfo visionDataInfo = new VisionDataInfo();
            visionDataInfo.setStudentId(studentInfoBean.getId());
            visionDataInfo.setLuoRightVal(luoData.getValueMap().get("right"));
            visionDataInfo.setLuoLeftVal(luoData.getValueMap().get("left"));

            visionDataInfo.setJiaoRightVal(jiaoData.getValueMap().get("right"));
            visionDataInfo.setJiaoLeftVal(jiaoData.getValueMap().get("left"));
            visionDataInfo.setCreateTime(System.currentTimeMillis());
            visionDataInfo.save();
            ToastUtils.showShort("添加成功");
            mViewModel.isChangeVision.setValue(true);
            obtainData();
        }

    }

    private void showAddView() {
        NiceDialog.init()
                .setLayoutId(R.layout.dialog_add_vision)
                .setConvertListener(new ViewConvertListener() {
                    @Override
                    public void convertView(ViewHolder holder, final BaseNiceDialog dialog) {
                        Button cancelBtn = holder.getView(R.id.cancel);
                        Button sureBtn = holder.getView(R.id.sure);
                        VisionContentView luoView = holder.getView(R.id.luo_view);
                        final ContentDataBean luoData = new ContentDataBean();
                        luoData.setTitleStr("裸眼视力");
                        luoView.setContentDataBean(luoData);
                        VisionContentView jiaoView = holder.getView(R.id.jiao_view);
                        final ContentDataBean jiaoData = new ContentDataBean();
                        jiaoData.setTitleStr("矫正视力");
                        jiaoView.setContentDataBean(jiaoData);
                        cancelBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                dialog.dismiss();
                            }
                        });
                        sureBtn.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (ObjectUtils.isEmpty(luoData.getValueMap()) && ObjectUtils.isEmpty(jiaoData.getValueMap())) {
                                    ToastUtils.showShort("请输入数据");
                                    return;
                                }
                                dialog.dismiss();
                                addVision(luoData, jiaoData);
                            }
                        });
                    }
                })
                .setOutCancel(false)
                .setAnimStyle(R.style.CenterDialogAnimation).show(getChildFragmentManager());
    }
}
